/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 */


package com.bc66.tiger.test.project.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bc66.tiger.dict.service.IEnumDictService;
import com.bc66.tiger.framework.base.BaseRestSpringController;
import com.bc66.tiger.framework.context.ApplicationContextHolder;
import com.bc66.tiger.framework.page.Page;
import com.bc66.tiger.framework.page.PageRequest;
import com.bc66.tiger.test.project.model.UserInfo;
import com.bc66.tiger.test.project.service.impl.UserInfoServiceImpl;

/**
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends BaseRestSpringController<UserInfo,java.lang.Long>{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private UserInfoServiceImpl userInfoService;
	
	private final String LIST_ACTION = "redirect:/userinfo";
		
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性
	 **/
	public void setUserInfoService(UserInfoServiceImpl userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	/** 列表 */
	@Override
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {
		PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
		//pageRequest.getFilters(); //add custom filters
		
		IEnumDictService enumDictService = (IEnumDictService)ApplicationContextHolder.getApplicationContext().getBean("enumDictService");
		System.out.println("GenderType:-------------------"+enumDictService.getDictDesc("GenderType", "1"));
		System.out.println("TestType:-------------------"+enumDictService.getDictDesc("TestType", "1"));
		System.out.println("UserType:-------------------"+enumDictService.getDictDesc("UserType", "1"));
		Page page = this.userInfoService.findByPageRequest(pageRequest);
		
		ModelAndView result = toModelAndView(page, pageRequest);
		result.addObject("userInfo",userInfo);
		result.setViewName("/userinfo/list");
		return result;
	}
	
	/** 进入新增 */
	@Override
	public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
		return new ModelAndView("/userinfo/new","userInfo",userInfo);
	}
	
	/** 显示 */
	@Override
	public ModelAndView show(@PathVariable java.lang.Long id) throws Exception {
		UserInfo userInfo = (UserInfo)userInfoService.getById(id);
		return new ModelAndView("/userinfo/show","userInfo",userInfo);
	}
	
	/** 编辑 */
	@Override
	public ModelAndView edit(@PathVariable java.lang.Long id) throws Exception {
		UserInfo userInfo = (UserInfo)userInfoService.getById(id);
		return new ModelAndView("/userinfo/edit","userInfo",userInfo);
	}
	
	/** 保存新增 */
	@Override
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
		userInfoService.save(userInfo);
		return new ModelAndView(LIST_ACTION);
	}
	
	/** 保存更新 */
	@Override
	public ModelAndView update(@PathVariable java.lang.Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
		UserInfo userInfo = (UserInfo)userInfoService.getById(id);
		bind(request,userInfo);
		userInfoService.update(userInfo);
		return new ModelAndView(LIST_ACTION);
	}
	
	/** 删除 */
	@Override
	public ModelAndView delete(@PathVariable java.lang.Long id) {
		userInfoService.removeById(id);
		return new ModelAndView(LIST_ACTION);
	}

	/** 批量删除 */
	@Override
	public ModelAndView batchDelete(java.lang.Long[] items) {
		for(int i = 0; i < items.length; i++) {
			userInfoService.removeById(items[i]);
		}
		return new ModelAndView(LIST_ACTION);
	}
	
}

